/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controls;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import others.Console;
import others.Login;
import servlets.BackupsXML;
import table.Request;
import table.WP;

/**
 *
 * @author Agata
 */
@ManagedBean
@RequestScoped
public class WPControl {
    
    private List<WP> list = null;
    private WP selectedElement = null;
    @ManagedProperty(value = "#{login}")
    private Login login;

    /** Creates a new instance of RequestControl */
    public WPControl() {
        list = new ArrayList<WP>();
        doList();
    }
    
     private void doList() {
        String resp = Console.request("c:\\Windows\\System32\\inetsrv\\appcmd list WP /xml");
        System.out.print(resp);
        // creating javaDom parser
        DocumentBuilderFactory builderFactory =
                DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = builderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(BackupsXML.class.getName()).log(Level.SEVERE, null, ex);
        }
        Document document = null;
        try {
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(resp));
            document = builder.parse(is);
        } catch (IOException ex) {
            Logger.getLogger(BackupControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(BackupsXML.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (document != null) {
            Element rootElement = document.getDocumentElement();
            NodeList nodes = rootElement.getChildNodes();

            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (node instanceof Element) {
                    //a child element to process
                    Element child = (Element) node;
                    list.add(new WP(child.getAttribute("WP.NAME"), child.getAttribute("APPPOOL.NAME")));
                }
            }
        }
    }

    public List<WP> getList() {
        return list;
    }

    public void setList(List<WP> list) {
        this.list = list;
    }

    public Login getLogin() {
        return login;
    }

    public void setLogin(Login login) {
        this.login = login;
    }

    public WP getSelectedElement() {
        return selectedElement;
    }

    public void setSelectedElement(WP selectedElement) {
        this.selectedElement = selectedElement;
    }
     
}
